package com.dameng.jianyouquan.interviewer.framgent;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.bumptech.glide.Glide;
import com.dameng.jianyouquan.R;
import com.dameng.jianyouquan.api.Constant;
import com.dameng.jianyouquan.bean.CommonBean;
import com.dameng.jianyouquan.bean.UserInfoBean;
import com.dameng.jianyouquan.api.NetWorkManager;
import com.dameng.jianyouquan.interviewer.activity.AuthenricationPersionActivity;
import com.dameng.jianyouquan.mvp.view.activity.LoginActivity;
import com.dameng.jianyouquan.utils.SpUtils;

import java.util.Objects;

import de.hdodenhof.circleimageview.CircleImageView;
import rx.Observer;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

public class MyInterviewerFramgent extends Fragment implements View.OnClickListener {


    private CircleImageView mCivHead;
    private TextView mAuthentication;
    private TextView mUserName;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_interviewer_my, null);
        mCivHead = view.findViewById(R.id.civ);

        mAuthentication = view.findViewById(R.id.tv_authentication);
        mUserName = view.findViewById(R.id.tv_username);


        mAuthentication.setOnClickListener(this);


        RelativeLayout rl_logout  = view.findViewById(R.id.rl_logout);
        rl_logout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                NetWorkManager.getService()
                        .getLogout()
                        .subscribeOn(Schedulers.io())
                        .observeOn(AndroidSchedulers.mainThread())
                        .subscribe(new Observer<CommonBean>() {
                            @Override
                            public void onCompleted() {

                            }

                            @Override
                            public void onError(Throwable e) {

                            }

                            @Override
                            public void onNext(CommonBean commonBean) {
                                int code = commonBean.getCode();
                                if (code==200){
                                    Context context = getContext();
                                    if (context!=null){
                                        SpUtils.setValue(context, Constant.SP_UID,"");
                                        SpUtils.setValue(context, Constant.SP_TOKEN,"");
                                        SpUtils.setValueInt(context, Constant.SP_ROLE_ID,0);
                                        SpUtils.setValueInt(context, Constant.SP_STATUS,0);
                                        SpUtils.setValue(context, Constant.SP_USERNAME,"");
                                        SpUtils.setValue(context, Constant.SP_USERPWD,"");
                                        SpUtils.setValue(context, Constant.SP_USERPWD,"");
                                        SpUtils.setValue(context, Constant.SP_WXOPENID,"");
                                        Objects.requireNonNull(getActivity()).finish();
                                        startActivity(new Intent(getActivity(), LoginActivity.class));
                                    }
                                }
                            }
                        });
            }
        });

        NetWorkManager.getService()
                .getUserInfo()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<UserInfoBean>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onNext(UserInfoBean userInfo) {
                        int code = userInfo.getCode();
                        if (code == 200) {
                            UserInfoBean.DataBean data = userInfo.getData();
                            UserInfoBean.DataBean.MainBean main = data.getMain();
                            UserInfoBean.DataBean.OtherBean other = data.getOther();
                            String userImg = other.getUserImg();
                            String username = other.getUsername();
                            mUserName.setText(username);
                            Glide.with(Objects.requireNonNull(getContext())).load(userImg).into(mCivHead);
                        } else {
                            String msg = userInfo.getMsg();

                        }
                    }
                });


        return view;
    }


    @Override
    public void onClick(View view) {
        int id = view.getId();
        switch (id){
            case R.id.tv_authentication:
                startActivity(new Intent(getActivity(), AuthenricationPersionActivity.class));
                break;
        }
    }
}
